#include "mydata.h"


MyData *MyData::obj = nullptr;

MyData::MyData(QObject *parent) : QObject(parent)
{
    initRoundList();
    initCarList();
    initJunctionList();
}

MyData *MyData::getObject()
{
    if(obj == nullptr)
        obj = new MyData;
    return obj;

}

int MyData::roundCount()
{
    return  roundList.length();
}

Round *MyData::getRound(int roundIndex)
{
    if(roundIndex >= roundList.length())
        return nullptr;
    return roundList.at(roundIndex);
}

int MyData::carCount()
{
    return carList.length();
}

Car *MyData::getCar(int carIndex)
{
    if(carIndex >= carList.length())
        return nullptr;
    return carList.at(carIndex);
}

int MyData::junctionCount()
{
    return  junctionList.length();
}

Junction *MyData::getJunction(int junctionIndex)
{
    if(junctionIndex >= junctionList.length())
        return nullptr;
    return junctionList.at(junctionIndex);
}

void MyData::updateCarPos(int carIndex, int roundIndex, int roundPos)
{
    if(roundIndex >= roundList.length())
        return;
    if(carIndex >= carList.length())
        return;
    if(roundPos>9)
        roundPos = 9;
    if(roundPos<0)
        roundPos = 0;
    //更新车辆坐标
    Round *r = roundList.at(roundIndex);
    int dir = r->dir;
    if(dir == 0)
    {//up
        int ny = r->y + r->h;
        int nx = r->x + r->w/2;
        int f = r->h / 9;
        ny = ny -f * roundPos;
        carList.at(carIndex)->x= nx-15;
        carList.at(carIndex)->y= ny-15;

    }
    if(dir == 1)
    {//down
        int ny = r->y;
        int nx = r->x + r->w/2;
        int f = r->h / 9;
        ny = ny + f * roundPos;
        carList.at(carIndex)->x=nx-15;
        carList.at(carIndex)->y=ny-15;

    }
    if(dir == 2)
    {//left
        int ny = r->y+r->h/2;
        int nx = r->x + r->w;
        int f = r->w / 9;
        nx = nx - f * roundPos;
        carList.at(carIndex)->x=nx-15;
        carList.at(carIndex)->y=ny-15;

    }
    if(dir == 3)
    {//right
        int ny = r->y+r->h/2;
        int nx = r->x ;
        int f = r->w/9;
        nx = nx + f * roundPos;
        carList.at(carIndex)->x=nx-15;
        carList.at(carIndex)->y=ny-15;

    }
    carList.at(carIndex)->roundIndex = roundIndex;
    carList.at(carIndex)->roundPos = roundPos;
    //更新流量道路控制
    updateRound();
}

void MyData::updateRound()
{
    for (int i=0;i<roundList.length();i++) {
        int count = 0;
        for(int j =0;j<carList.length();j++)
        {
            if(carList.at(j)->roundIndex == i)
                count++;
        }
        if(count == 1)
            roundList.at(i)->color = QColor(Qt::yellow);
        else if (count>=2) {
            roundList.at(i)->color = QColor(Qt::red);
        }
        else
            roundList.at(i)->color = QColor(Qt::green);

    }
}

void MyData::updatenewLampColorSlot(int junctionId, int lampId, int color)
{
    if(junctionId>=junctionList.length())
        return;
    if(lampId >= junctionList.at(junctionId)->lampCount())
        return;
    if(color == 0)
        junctionList.at(junctionId)->getLamp(lampId)->color = QColor(Qt::green);
    else if (color == 1) {
        junctionList.at(junctionId)->getLamp(lampId)->color = QColor(Qt::yellow);
    }
    else if (color == 2) {
        junctionList.at(junctionId)->getLamp(lampId)->color = QColor(Qt::red);
    }
    else if (color == 3) {
        junctionList.at(junctionId)->getLamp(lampId)->color = QColor(Qt::white);
    }

}

void MyData::initRoundList()
{
    roundList.append(new Round(96, 27, 648, 3,2,QColor(Qt::green)));
    roundList.append(new Round(96, 59, 648, 3,3,QColor(Qt::green)));
    roundList.append(new Round(96, 347, 648, 3,2,QColor(Qt::green)));
    roundList.append(new Round(96, 379, 648, 3,3,QColor(Qt::green)));
    roundList.append(new Round(96, 671, 648, 3,2,QColor(Qt::green)));
    roundList.append(new Round(96, 703, 648, 3,3,QColor(Qt::green)));
    roundList.append(new Round(27, 96, 3, 220,1,QColor(Qt::green)));
    roundList.append(new Round(59, 96, 3, 220,0,QColor(Qt::green)));
    roundList.append(new Round(783, 96, 3, 220,1,QColor(Qt::green)));
    roundList.append(new Round(815, 96, 3, 220,0,QColor(Qt::green)));
    roundList.append(new Round(27, 420, 3, 220,1,QColor(Qt::green)));
    roundList.append(new Round(59,420, 3, 220,0,QColor(Qt::green)));
    roundList.append(new Round(783, 420, 3, 220,1,QColor(Qt::green)));
    roundList.append(new Round(815, 420, 3, 220,0,QColor(Qt::green)));



}

void MyData::initCarList()
{
    carList.append(new Car(623,555,-1,-1,0));
    carList.append(new Car(623,585,-1,-1,1));
}

void MyData::initJunctionList()
{
    Junction *j1, *j2;
    j1 = new Junction;
    j2 = new Junction;
    j1->addLamp(new SignalLamp(QColor(Qt::red),0,2));
    j1->addLamp(new SignalLamp(QColor(Qt::red),1,6));
    j1->addLamp(new SignalLamp(QColor(Qt::red),1,11));
    junctionList.append(j1);
    j2->addLamp(new SignalLamp(QColor(Qt::red),0,3));
    j2->addLamp(new SignalLamp(QColor(Qt::red),1,8));
    j2->addLamp(new SignalLamp(QColor(Qt::red),1,13));
    junctionList.append(j2);
}

Round::Round(int x, int y, int w, int h, int dir, QColor c)
    : x(x),y(y),w(w),h(h),dir(dir),color(c)
{

}

Car::Car(int x, int y, int roundIndex, int roundPos, int id)
    : x(x),y(y),roundIndex(roundIndex),roundPos(roundPos),id(id)
{

}

SignalLamp::SignalLamp(QColor c, int group, int roundIndex)
        : color(c),group(group),roundIndex(roundIndex)
{

}

void Junction::addLamp(SignalLamp *lamp)
{
    lampList.append(lamp);
}

int Junction::lampCount()
{
    return lampList.length();
}

int Junction::lampGroupCount()
{
    bool is0 = false, is1 = false;
    for (int i = 0; i < lampList.length(); i++)
    {
           if(lampList.at(i)->group == 0)
               is0=true;
           else if (lampList.at(i)->group == 1) {
               is1=true;
           }
    }
    if(is0 && is1)
        return 2;
    else if (is0 || is1)
        return  1;
    return 0;
}

SignalLamp *Junction::getLamp(int lampIndex)
{
 if(lampIndex>=lampList.length())
     return nullptr;
 return lampList.at(lampIndex);
}
